package com.example.se2_funmessages;

import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import com.example.se2_funmessages.SendMessage.SendMessageTask;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class NewMessage extends Activity{
	
	private static final String EXTRA_MESSAGE = null;

	ListView listview;
	
	Context context = this;
	
	public static ArrayAdapter<Message> messages;
	
	String userNameTo = null;
	String userNameFrom = null;
	
	
	@Override
	protected void onResume() {
	    super.onResume();
	    new GetMessages().execute(this);
	    // Normal case behavior follows
	}
    
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_new_message);
		
		
		listview = (ListView)findViewById(R.id.listView1);
		
		
	    
		new GetMessages().execute(this);
		
		listview.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long arg3)
			{

				Message clickedMessage = messages.getItem(position);
				
				try {
					 clickedMessage.To_User = new getUserName().execute(clickedMessage.To_Id).get();
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (ExecutionException e) {
					e.printStackTrace();
				}
				try {
					clickedMessage.From_User = new getUserName().execute(clickedMessage.From_Id).get();
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (ExecutionException e) {
					e.printStackTrace();
				}
				
				viewMessage(clickedMessage);
			}	
		});

	
	}
	
	public void viewMessage(Message viewMessage)
	{
		Intent intent = new Intent(this, ViewMessage.class);
		intent.putExtra(EXTRA_MESSAGE, viewMessage.compact());
		startActivity(intent);
	}
	
public class GetMessages extends AsyncTask<Context, Void, ArrayList<Message>> {


		
		
		@Override
		protected ArrayList<Message> doInBackground(Context... params) {
			
			DatabaseHandler db = new DatabaseHandler(params[0]);
			
			int userNum = Login.currentUserId;
			
			return (ArrayList<Message>)db.getAll(userNum);
			
			
			
		}
		
		@Override
		protected void onPostExecute(ArrayList<Message> values)
		{
			
			messages = new ArrayAdapter<Message>(context, R.layout.list_item, R.id.contactItem, values);
		        
			listview.setAdapter(messages);
		
				

			
		}
}

@Override
public void onContentChanged() {
    super.onContentChanged();
 
    View empty = findViewById(R.id.empty2);
    ListView list = (ListView) findViewById(R.id.listView1);
    list.setEmptyView(empty);
}

public class getUserName extends AsyncTask<Integer, Void, String> {
	
	@Override
	protected String doInBackground(Integer... params) {
					
		HttpClient httpclient = new DefaultHttpClient();
		

	    HttpPost httppost = new HttpPost("http://softeng.cs.uwosh.edu/students/cs342g3/getUserName.php?id="+params[0]);
	    

	    try {

	        // Execute HTTP Post Request

	        ResponseHandler<String> responseHandler = new BasicResponseHandler();
	        String response = httpclient.execute(httppost, responseHandler);
	        String Response = response;

	        return Response;

	        //This is the response from a php application


	    } catch (ClientProtocolException e) {
	        //Toast.makeText(params[0], "CPE response " + e.toString(), Toast.LENGTH_LONG).show();
	        // TODO Auto-generated catch block
	    } catch (IOException e) {
	        //Toast.makeText(params[0], "IOE response " + e.toString(), Toast.LENGTH_LONG).show();
	        // TODO Auto-generated catch block
	    }
		return null;
		
	} // end method doInBackground
}

}